package com.example.board.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.board.pojo.User;
import com.example.board.pojo.dto.UserRecordDto;
import com.example.board.pojo.vo.IndustryShareVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface OperatorAnalysisMapper extends BaseMapper<User> {

    @Select({
            "SELECT count(distinct o.user_record_id) FROM sd_order o  ",
            "WHERE o.tool_man_id is not null and o.user_record_id is not null ",
            " and o.on_cancel = 0 ",
            "and o.order_state in (2,3,12) and o.order_time >= #{startMoth} ",
    })
    String findAllDetail3(@Param("startMoth") LocalDateTime startMoth);

    @Select({
            "<script>",

            "SELECT count(o.id)  customerNumber,o.receiver_shop_class_name typeName ",
            " FROM sd_order o ",
            "<where> 1=1  ",

            "<if test='city != null and city != \"\" '> ",
            "and o.receiver_city = #{city} ",
            "</if> ",

            "<if test='province != null and province != \"\" '> ",
            "and o.receiver_province = #{province} ",
            "</if> ",
            "and o.order_state = 3  and o.order_time BETWEEN #{startMonth} and #{endMonth} ",
            "</where>",
            "GROUP BY o.receiver_shop_class_name  ",
            "ORDER BY count(o.id) desc ",

            "</script> "
    })
    List<IndustryShareVo> findAllDealNow(@Param("startMonth") LocalDateTime startMonth,
                                         @Param("endMonth") LocalDateTime endMonth,
                                         @Param("province") String province,
                                         @Param("city") String region);


    @Select({
            "SELECT o.receiver_shop_class_name typeName ",
            " FROM sd_order o where o.receiver_shop_class_name is not null ",
            " and o.receiver_province = #{province} and o.receiver_city = #{city}  ",
            "GROUP BY o.receiver_shop_class_name  ",
    })
    List<String> findAllTypeName( @Param("province") String province,
                                  @Param("city") String region);

    @Select({
            "<script>",

            "SELECT count(o.id)  customerNumber ,o.receiver_shop_class_name shopClass ",
            " FROM sd_order o ",
            "<where> 1= 1  ",
            "<if test='region != null and region != \"\" '> ",
            "and o.receiver_city = #{region} ",
            "</if> ",

            "<if test='province != null and province != \"\" '> ",
            "and o.receiver_province = #{province} ",
            "</if> ",

            "and o.order_state = 3  and o.order_time BETWEEN #{startMonth} and #{endMonth} ",

            "and  o.receiver_shop_class_name in  ",
            " <foreach collection=\"type\" item=\"type\" index=\"index\" open=\"(\" separator=\",\" close=\")\">",
            "#{type}",
            "</foreach>",

            "</where>",
            " GROUP BY  o.receiver_shop_class_name ",

            "</script> "

    })
    List<UserRecordDto> findTypeNow(@Param("startMonth") LocalDateTime startMonth,
                                    @Param("endMonth") LocalDateTime endMonth,
                                    @Param("province") String province,
                                    @Param("region") String region,
                                    @Param("type") List<String>  type);


    @Select({
            "<script>",
            "SELECT count(o.id)  customerNumber ",
            " FROM sd_order o ",
            "<where> 1=1 ",
            "<if test='region != null and region != \"\" '> ",
            "and o.receiver_city = #{region} ",
            "</if> ",

            "<if test='province != null and province != \"\" '> ",
            "and o.receiver_province = #{province} ",
            "</if> ",

            "and o.order_state = 3  and o.order_time BETWEEN #{startMonth} and #{endMonth} ",

            " </where> ",
            "</script> "

    })
    Integer findTypeAllNow(@Param("startMonth") LocalDateTime startMonth,
                           @Param("endMonth") LocalDateTime endMonth,
                           @Param("province") String province,
                           @Param("region") String region);


}
